﻿
using System.Windows;
using System.Windows.Media;
using Perforce.P4;
using Red.Controls.Utils;
using Red.Core;

namespace Red.Controls.Depot.ViewModels
{
    /// <summary>
    /// 
    /// </summary>
    public class FileViewItem : ViewModel
    {
        #region Data

        private readonly string _title;
        private readonly string _group;
        private readonly string _path;
        private readonly ImageSource _image;

        private bool _isSelected;
        private bool _isEnabled;
        protected FileMetaData _p4MetaData;
        private P4FileState _p4FileState;
        private bool _isOutOfDate;
        private bool _isUpdating;
        private string _description;

        #endregion

        #region Properties

        public bool IsSelected
        {
            get { return _isSelected; }
            
            set 
            {
                if ( value != _isSelected )
                {
                    _isSelected = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool IsEnabled
        {
            get { return _isEnabled; }

            set
            {
                if ( value != _isEnabled )
                {
                    _isEnabled = value;
                    OnPropertyChanged();
                }
            }
        }

        public string Title 
        { 
            get { return _title; }
        }

        public string Group
        {
            get { return _group; }
        }

        public ImageSource Image
        {
            get { return _image; }
        }

        public string Path
        {
            get { return _path; }
        }

        public FileMetaData P4MetaData
        {
            get { return _p4MetaData; }
            set
            {
                if ( _p4MetaData == value )
                    return;

                _p4MetaData = value;
                OnP4MetaDataChanged();
            }
        }

        public P4FileState P4FileState
        {
            get { return _p4FileState; }
            set
            {
                if ( _p4FileState == value )
                    return;

                _p4FileState = value;
                OnPropertyChanged();
            }
        }

        public bool IsOutOfDate
        {
            get { return _isOutOfDate; }
            set
            {
                if ( _isOutOfDate == value )
                    return;

                _isOutOfDate = value;
                OnPropertyChanged();
            }
        }

        public bool IsInDepot
        {
            get { return _p4FileState != P4FileState.P4FS_NotInDepot; }
        }

        public bool IsUpdating
        {
            get { return _isUpdating; }
            set
            {
                if ( _isUpdating == value )
                    return;

                _isUpdating = value;
                OnPropertyChanged();
            }
        }

        public string Description
        {
            get { return _description; }
            set
            {
                if ( _description == value )
                    return;

                _description = value;
                OnPropertyChanged();
            }
        }

        #endregion

        #region C-tor

        public FileViewItem( string path, string title, ImageSource image, string group, bool isEnabled )
        {
            _path = path;
            _title = title;
            _image = image;
            _group = group;
            _p4FileState = P4FileState.P4FS_NotInDepot;
            _isEnabled = isEnabled;
        }

        #endregion C-tor

        #region Public methods

        /// <summary>
        /// Returns true only for files because for Perforce they are the only items on which we can perform version control operations such as: submit, mark for add and checkout.
        /// Returns false for directories because for Perforce they don't exist. Perforce keeps only paths to files.        
        /// </summary> 
        public virtual bool IsVersionControlItem()
        {
            return false;
        }

        /// <summary>
        /// This method returns path which is used in Get Latest operation.
        /// Returns Path for each file.
        /// Returns Path + "\..." for operations on directories, as this is the syntax which is accepted by Perforce.
        /// "\..." is a Perforce equivalent for *.* wildcard.
        /// </summary> 
        public virtual string GetP4SyncPath()
        {
            return Path;
        }

        /// <summary>
        ///     Returns data that is then dragged as a part of drag-and-drop operation
        /// </summary>
        /// <returns>
        ///     Dragged data, if the item is meant to be draggable, or null (default)
        /// </returns>
        public virtual IDataObject GetDraggedData()
        {
            return null;
        }

        #endregion Public methods

        #region Protected methods

        protected virtual void OnP4MetaDataChanged()
        {

        }

        #endregion Protected methods
    }
   
}
